<template>
  <el-select v-model="underlyerIds" placeholder="请选择标的"
             size="small"
             filterable="true" clearable remote
             :style="{width: '100%'}"
             :remote-method="queryUnderlyers"
             @change="underlyerIdsSelectChange"
             :loading="loading">
    <el-option
        v-for="item in underlyerIdsList"
        :key="item.code"
        :label="item.productName"
        :value="item.instrument"
        :disabled="item.disabled"
    />
  </el-select>

</template>
<script>
import {getInstrumentList, getDailyPrice} from '@/api/calc/otcOption/calc'
import {isNotVoid, isVoid} from '@/utils/util'
import {debounce} from 'lodash'

export default {
  model: {
    prop: 'selectedUnderlyerIds', // 默认值value，如果props中用value接收来自父组件的v-model值，那么这里可以不写prop
  },
  props: {
    underlyerIdsList: Array,
    coverUnderlyerList: Array, //用于覆盖标的查询结果，常用于外部筛选联动
    underlyerIds: String,
    selectedUnderlyerIds: String // model.prop为selectedYear，这里就用selectedYear接收，默认是'value'
  },
  watch: {
    underlyerIds: {
      handler(newVal) {
      }
    }
  },
  data() {
    return {
      underlyerIds: this.selectedUnderlyerIds,
      underlyerIdsList: []
    }
  },
  mounted() {
    setTimeout(() => {
      this.getInstrumentList(this.$attrs.modelValue)
    }, 100)
  },
  methods: {
    change(v) {
    },
    setValue(query) {
      this.underlyerIds = query
      this.underlyerIdsSelectChange(query)
    },
    underlyerIdsSelectChange(query) {
      this.getInstrumentList(query)
    },
    queryUnderlyers(query) {
      this.getInstrumentList(query)
    },
    queryUnderlyersByCoverList(query, coverList) {
      var param = {}
      if (isVoid(coverList) || coverList.length < 1) {
        if (isNotVoid(query)) {
          if (query.length > 1) {
            param.condition = query
            getInstrumentList(param).then(response => {
              this.underlyerIdsList = response
            })
          } else {
            this.underlyerIdsList = []
          }
        } else {
          this.underlyerIdsList = []
        }
      } else {
        this.underlyerIdsList = coverList
      }
    },
    /** 查询字典类型列表 */
    getInstrumentList: debounce(
        function (query) {
          var param = {}
          if (isVoid(this.coverUnderlyerList) || this.coverUnderlyerList.length < 1) {
            if (isNotVoid(query)) {
              if (query.length > 1) {
                param.condition = query
                getInstrumentList(param).then(response => {
                  this.underlyerIdsList = response
                })
              }
            }
          } else {
            this.underlyerIdsList = this.coverUnderlyerList
          }
        }, 500
    ),
    /** 查询价格类型类型列表 */
    async getDailyPrice(query) {
      let instrumentId = query.instrumentId
      let strings = instrumentId.split(',')
      var param = {
        iCode: strings[0],
        aType: strings[1],
        mType: strings[2],
        begDate: query.startDate
      }
      return await getDailyPrice(param).then(response => {
        /*
        * "dpOpen"
          "dpClose"
          "dpHigh"
          "dpLow"
          "dpAvg"
        * */
        // return this.lsh.ceil(response.result.dpClose,4);
        /*
      dpClose    1
      dpSet      2
      dpAvg      3
      价格类型可选list  【0,1，2】
     * */
        let result = response.result
        if (response.result == null) {
          return false
        }
        let filterList = ['0']
        if (!Boolean(result)) return {result: {dpClose: 0}, filterList: filterList}
        if (Boolean((result.dpAvg))) filterList.push(3)
        if (Boolean((result.dpSet))) filterList.push(2)
        if (Boolean(result.dpClose)) filterList.push(1)
        result.filterList = filterList.toString()
        return result
      })
    }
  },
  unmounted() {
    // 移除组件时，取消定时器
    // this.getInstrumentList.cancel()
  }
}
</script>
